package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Reference
    private MemberService memberService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        try {
            List<Setmeal> setmealList = setmealService.getSetmeal();
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS,setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setmealService.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }



    }


    //获取预约记录
    @RequestMapping("/findMemberBySetmeal")
    public Result findMemberBySetmeal(HttpServletRequest request){

        System.out.println("你好");


        //1.使用cookie获取当前用户标识，获取手机号码
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            //2.判断获取的cookie是否正确
            if ("login_member_telephone".equals(cookie.getName())){
                //3.获取cookie对象中的value，进行写出
                String value = cookie.getValue();

              List<Map<String,Object>> list = setmealService.findMemberBySetmeal(value);

              return new Result(true,MessageConstant.GET_ORDER_RECORD_LIST_SUCCESS,list);

            }
        }
        return new Result(false,MessageConstant.GET_ORDER_RECORD_LIST_FAIL);
    }


    //根据预约id获取套餐对象
    @RequestMapping("/findSetmealById")
    public Result findSetmealById(Integer id){

        try {
            Setmeal setmeal = setmealService.findSetmealById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //根据日期范围查询当前会员历史预约记录的功能
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody String[] times,HttpServletRequest request){
        System.out.println(times);
        //将时间数组传给sql进行查询处理
        try {

            List<Map<String,Object>> list = null;
            //1.使用cookie获取当前用户标识，获取手机号码
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                //2.判断获取的cookie是否正确
                if ("login_member_telephone".equals(cookie.getName())){
                    //3.获取cookie对象中的value，进行写出
                    String telePhone = cookie.getValue();
                    list = setmealService.findByPage(times,telePhone);

                }
            }

            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }























        //获取预约记录
    @RequestMapping("/findMemberBySetmea2")
    public Result findMemberBySetmeal2() {


        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map =   new HashMap<String,Object>();
        map.put("name","胡八一");
        map.put("setmealname","入职体检");
        list.add(map);

        Map<String,Object> map2 =   new HashMap<String,Object>();
        map2.put("name","胡八二");
        map2.put("setmealname","入职体检2");
        list.add(map2);

        Map<String,Object> map3 =   new HashMap<String,Object>();
        map3.put("name","胡八三");
        map3.put("setmealname","入职体检3");
        list.add(map3);

        return new Result(true,MessageConstant.GET_ORDER_RECORD_LIST_SUCCESS,list);

    }

    public void test(){
    /*    try {
            Map<String,Object> map = setmealService.findMemberBySetmeal();
            return new Result(true,MessageConstant.GET_ORDER_RECORD_LIST_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDER_RECORD_LIST_FAIL);
        }*/

    }


}
